package com.leyou.item.controller;

import com.github.pagehelper.Page;

import com.leyou.common.constant.PageResult;
import com.leyou.item.domian.Spu;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.bouncycastle.asn1.esf.SPUserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


   //根据分类查找商品---分页查询商品SPU信息
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>>goodsPageQuery(@RequestParam(value = "page",defaultValue = "5") Integer page,
                                                            @RequestParam(value = "rows",defaultValue = "1") Integer rows,
                                                            @RequestParam(value = "key",required =false ) String key,
                                                            @RequestParam(value = "saleable",required = false) Boolean saleable){


        PageResult<SpuDTO>list=goodsService.goodsPageQuery(page, rows,key,saleable);
      return ResponseEntity.ok(list);



    }

    //添加商品
    @PostMapping("/goods")
    public ResponseEntity<Void>saveGoods(@RequestBody SpuDTO spuDTO ){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }


    //第六天代码
    //商品下架处理（先要下架才能允许修改）
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void>updateSaleable(@RequestParam(value = "id") Long id,@RequestParam
            (value = "saleable") Boolean saleable){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();


    }

    //根据spu的id查询spuDetail对象(用于回显)
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO>findSpuDetailBySpuId(@RequestParam("id") Long id){
        SpuDetailDTO spuDetailDTO=goodsService.findSpuDetailBySpuId(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

   // 根据SpuId查询Sku集合(用于回显)
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>findSkusBySpuId(@RequestParam("id")Long id){
        List<SkuDTO>list=goodsService.findSkusBySpuId(id);
        return ResponseEntity.ok(list);

    }

   //修改商品
    @PutMapping("/goods")
    public ResponseEntity<Void>updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    //根据spuId查询Spu对象，要给对象中的sku集合和detail对象赋值
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuBySpuId(@PathVariable("id") Long id){
        SpuDTO spuDTO = goodsService.findSpuBySpuId(id);
        return ResponseEntity.ok(spuDTO);
    }

    //在item微服务提供根据sku的id集合查询sku的对象集合的处理
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> findSkusBySkuIds(@RequestParam("ids") List<Long> ids){
        List<SkuDTO> list = goodsService.findSkusBySkuIds(ids);
        return ResponseEntity.ok(list);
    }


    /**
     * 减库存
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> map){
        goodsService.minusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
